package com.iceph0x.android;

import java.util.ArrayList;
import java.util.List;

import com.iceph0x.android.aidl.IDataHandler;
import com.iceph0x.android.aidl.IMyService;
import com.iceph0x.android.aidl.Person;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;

public class PublicService extends Service {
    
    private List<Person> persons = new ArrayList<Person>();
    
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        System.out.println("public service start!");
    }
    
    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }
    
    private final IMyService.Stub mBinder = new IMyService.Stub() {
        
        @Override
        public IDataHandler getDataHandler() throws RemoteException {
            System.out.println("public service::" + Process.myPid() + "," + Thread.currentThread().getId()
                    + "@get handler");
            return handler;
        }
        
        @Override
        public List<Person> getAllPerson() throws RemoteException {
            System.out.println("public service::" + Process.myPid() + "," + Thread.currentThread().getId()
                    + "@get all person");
            return persons;
        }

        @Override
        public void addPerson(Person person) throws RemoteException {
            System.out.println("public service::" + Process.myPid() + "," + Thread.currentThread().getId()
                    + "@add person");
            if (person != null)
                persons.add(person);
        }
    };
    
    private final IDataHandler.Stub handler = new IDataHandler.Stub() {
        @Override
        public void handle(Person person) throws RemoteException {
            System.out.println("public service::" + Process.myPid() + "," + Thread.currentThread().getId()
                    + "@handle person");
            System.out.println("person::" + person.toString());
        }
    };
}